package com.hs.zhou.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.ding.utils.NumberGeneratorUtils;
import com.hs.dto.PageBean;
import com.hs.dto.ResultBean;
import com.hs.zhou.pojo.Bill;
import com.hs.zhou.pojo.BillStatement;
import com.hs.zhou.service.IBillStatementService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;



/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xudongzhou
 * @since 2022-06-20
 */
@CrossOrigin
@RestController
@RequestMapping("/billStatement")
public class BillStatementController {

    @Autowired
    private IBillStatementService billStatementService;

    @GetMapping("lists")
    public ResultBean queryAll(Integer currentPage, Integer pageSize,String billStatementNo,Integer billStatuss){

        IPage<BillStatement> page = new Page<>(currentPage,pageSize);

        QueryWrapper<BillStatement> qr = new QueryWrapper<>();
        if(!ObjectUtils.isEmpty(billStatementNo)){
            qr.eq("bill_statement_no",billStatementNo);
        }
        if(!ObjectUtils.isEmpty(billStatuss) && billStatuss != 2){
            qr.eq("status",billStatuss);
        }

        IPage<BillStatement> statementIPage = billStatementService.page(page,qr);

        List<BillStatement> records = statementIPage.getRecords();

        Long total = statementIPage.getTotal();

        PageBean<BillStatement> pageBean = new PageBean<>();

        if(statementIPage != null){
            pageBean.setList(records);
            pageBean.setTotal(total.intValue());

            ResultBean ok = ResultBean.OK("查询成功");
            ok.setData(pageBean);

            return  ok;
        }
        return ResultBean.ERROR("查询失败");
    }

    @PostMapping
    public ResultBean addBillStatement(@RequestBody List<Bill> arr) throws ParseException {


        Float totalsum = 0.0f;
        String name = "";
        for(Bill bill:arr){

            totalsum += bill.getExpenseinfo().getTotalReceivable();

            name = bill.getOrder().getCustomerName();
        }

        BillStatement billStatement = new BillStatement();
        billStatement.setBillStatementNo(NumberGeneratorUtils.randomNumber());
        billStatement.setCustomerName(name);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String Time=df.format(new Date());// new Date()为获取当前系统时间
        Date date = df.parse(Time);
        billStatement.setCreateTime(date);
        billStatement.setOrderNumber(arr.size());
        billStatement.setTotalReceive(totalsum);

        boolean b = billStatementService.save(billStatement);

        if(b){
            return ResultBean.OK("生成成功!");
        }
        return ResultBean.ERROR("生成失败");
    }

    @PutMapping
    public ResultBean changeStatus(@RequestBody List<BillStatement> arr){
        System.out.println(arr);

        boolean b = false;

        for(BillStatement billStatement:arr){
            billStatement.setStatus(1);
            b = billStatementService.saveOrUpdate(billStatement);
        }

        if(b){
            return ResultBean.OK("确认成功!");
        }
        return ResultBean.ERROR("确认失败");

    }

}
